package org.hot100_3;


public class leetcode2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode dummy = new ListNode(-1);
        ListNode node = dummy;

        int carry = 0;

        while (l1 != null || l2 != null) {

            int val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;

            carry = val / 10;
            val = val % 10;

            node.next = new ListNode(val);

            node = node.next;

            if (l1 != null) {
                l1 = l1.next;
            }

            if (l2 != null) {
                l2 = l2.next;
            }
        }

        if (carry!=0){
            node.next=new ListNode(carry);
        }

        return dummy.next;
    }
}
